package main.com.utils;

import main.com.dao.UserDao;

/**
 * @author lk
 * @date 2020/6/23 16:09
 * @description:
 */
public class CheckUtil {

    /**
     * 检验用户名合法性
     *
     * @param username
     * @return
     */
    public static String checkUserName(String username) {
        String f = "ok";
        UserDao userDao = new UserDao();
        if ("".equals(username)) {
            f = "用户名不能为空";
        } else if (username.length() < 6 || username.length() >= 10) {
            f = "用户名长度必须大于等于6位，小于10位";
        } else if (userDao.checkUser(username) != null) {
            f = "该用户名已被注册";
        } else {
            int j = 0;
            if ((username.replaceAll("\\d", "")
                    .replaceAll("[A-Z]", "")
                    .replaceAll("[a-z]", "")).length() > 0){
                f = "用户名只能由数字和英文字母组成";
                return f;
            }
            if (username.matches(".*\\d+.*")) j++;
            if (username.matches(".*[A-Za-z]+.*")) j++;
            if (j != 2)
                f = "用户名由数字和英文字母共同组成";
        }
        return f;
    }

    /**
     * 检验密码合法性
     *
     * @param password
     * @return
     */
    public static String checkPassWord(String password, String repassword) {
        String f = "ok";
        if ("".equals(password)) {
            f = "密码不能为空";
        } else if (!password.equals(repassword)) {
            f = "两次密码不一致";
        } else if (password.length() <= 6 || password.length() >= 12) {
            f = "密码长度必须大于6位，小于12位";
        } else {
            int j = 0;
            if (password.matches(".*\\d+.*")) j++;
            if (password.matches(".*[A-Z]+.*")) j++;
            if (password.matches(".*[a-z]+.*")) j++;
            if (j != 3)
                f = "密码至少有1个数字、1个大写字母和1个小写字母";
        }
        return f;
    }

    /**
     * 检验邮件地址合法性
     *
     * @param email
     * @return
     */
    public static String checkEmail(String email) {
        String f = "ok";
        UserDao userDao = new UserDao();
        if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
            f = "邮件格式不正确";
        } else if (userDao.checkEmail(email) != null) {
            f = "邮件地址不能重复";
        }
        return f;
    }

    /**
     * 检验工作单位合法性
     *
     * @param workplace
     * @return
     */
    public static String checkWorkPlace(String workplace) {
        String f = "ok";
        if ("".equals(workplace)) {
            f = "ok";
        } else if (workplace.length() > 50) {
            f = "单位最多50位";
        } else {
            int j = 0;
            if (workplace.matches(".*[\\u4E00-\\u9FA5]+.*")) j++;
            if (workplace.matches(".*[a-zA-Z]+.*")) j++;
            if (j != 2)
                f = "单位由中文和英文组成";

        }
        return f;
    }

}
